package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import entity.AjaxResult;
import entity.Criteria;
import entity.Goods;
import entity.UserFootCriteria;
import entity.UserFootGoods;
import service.impl.GoodsServiceImpl;


@Controller
public class GoodsController {

	@Autowired
	private GoodsServiceImpl serviceImpl;
	//首页数据加载
	@RequestMapping("findGoodsCriteria")
	@ResponseBody
	public List<Goods> findGoodsCriteria(@RequestParam(defaultValue="1") Integer page,
																Integer gid,
																String gname,
														@RequestParam(defaultValue="1")	Integer gtype){
		//计算start与limit
		Criteria cr=new Criteria();
		Integer start=(page-1)*4;
		Integer offset=4;
		//将计算的分页拼接到条件对象
		cr.setOffset(offset);
		cr.setStart(start);
		cr.setGname(gname);
		cr.setGid(gid);
		cr.setGtype(gtype);
		//执行查询
		List<Goods> list=serviceImpl.findGoodsCriteria(cr);
		return list;
																	
		
	}
	
	
	@RequestMapping("findGoodsTypeList")
	@ResponseBody
	public AjaxResult<Goods> findGoodsTypeList(@RequestParam (defaultValue="1")Integer page,
										@RequestParam (defaultValue="1")Integer gtype){
		
		//计算分页  每页10条
		Integer start=(page-1)*10;
		Integer offset=10;
		//创建条件对象
		Criteria cr=new Criteria();
		//设置条件  分类+分页
		cr.setGtype(gtype);
		cr.setStart(start);
		cr.setOffset(offset);
		//执行查询
		List<Goods> list=serviceImpl.findGoodsCriteria(cr);
		//查询总数
		Integer count=serviceImpl.getCount();
		//包装返回对象
		AjaxResult<Goods> ajax=new AjaxResult<Goods>();
		ajax.setList(list);
		ajax.setCount(count);
		return ajax;
	}
	
	
	//http://localhost:8080/heima/getUserFoot?uid=1&page=1
	@RequestMapping("getUserFoot")
	@ResponseBody
	public AjaxResult<UserFootGoods> getUserFoot(Integer uid,
								@RequestParam(defaultValue="1") Integer page ){
		
		//计算分页  每页10条
		Integer start=(page-1)*5;
		Integer offset=5;
		//创建条件对象
		UserFootCriteria cr=new UserFootCriteria();
		//设置条件
		cr.setUid(uid);
		cr.setStart(start);
		cr.setOffset(offset);
		//执行查询
		List<UserFootGoods> list=serviceImpl.selectUserFoot(cr);
		//创建返回对象
		AjaxResult<UserFootGoods> ajax=new AjaxResult<UserFootGoods>();
		//将查询到的用户浏览历史数据进行包装
		ajax.setList(list);
		ajax.setCount(list.size());
		ajax.setMsg("success");
		//返回数据
		return ajax;
	}
	
}
